package com.ups.singlearner.activities;

import com.ups.singlearner.ComposedNote;
import com.ups.singlearner.Composition;
import com.ups.singlearner.R;
import com.ups.singlearner.util.PlaySoundUtil;
import com.ups.singlearner.views.CustomSurfaceView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CompositionActivity extends Activity {

	private final int RECORDER_ACTIVITY = 0;
	private final String TAG = "CompositionActivity";

	private Context context = this;
	private Button playSoundButton;
	public static Button recordButton;
	private Button clearButton;
	private static CustomSurfaceView surfaceView;
	public static TextView resultatView = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compose);

		surfaceView = (CustomSurfaceView) findViewById(R.id.CustomSurfaceView1);

		resultatView = (TextView) findViewById(R.id.textView1);
		
		clearButton = (Button) findViewById(R.id.clearButton);
		clearButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// remove all composed notes
				Composition.getInstance().clear();
				// TODO find a proper way to redraw the canvas
				// instead of this : in onDraw
				// Paint paint = new Paint();
				// paint.setColor(Color.BLACK);
				// canvas.drawRect(new Rect(0, 0, canvas.getWidth(),
				// canvas.getHeight()), paint);
				resultatView.setText("");
				surfaceView.postInvalidate();
			}
		});
		
		playSoundButton = (Button) findViewById(R.id.playButton);
		playSoundButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				//playSound(R.raw.notes_de_musique);
				// play composition
				PlaySoundUtil.playComposition(Composition.getInstance());
			}
		});

		recordButton = (Button) findViewById(R.id.recordButton);
		recordButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent soundRecorderActivity = new Intent(context,
						SoundRecorderActivity.class);
				startActivityForResult(soundRecorderActivity, RECORDER_ACTIVITY);
				surfaceView.getThread().set_run(false);
			}
		});
	}

	private MediaPlayer mPlayer = null;
	
	private void playSound(int resId) {
		if(mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();
		}
		mPlayer = MediaPlayer.create(this, resId);
		mPlayer.start();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == RECORDER_ACTIVITY) {
			// return from record activity, draw result
			Log.d(TAG, "Get result !");
			for (ComposedNote n : Composition.getInstance().getSungNotes()) {
				Log.d(TAG, "note :" + n.getNote().name());
			}

			// assert(surfaceView != null);
			// surfaceView = new CustomSurfaceView(this, this.);
			recordButton.setEnabled(false);

			surfaceView.setDraw_result(true);
			surfaceView.postInvalidate();
		}
	}
}